package com.shudai.largescreen.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.shudai.largescreen.mapper.OperatorAnalysisMapper;
import com.shudai.largescreen.pojo.vo.IndustryShareDetailVo;
import com.shudai.largescreen.pojo.vo.IndustryShareVo;
import com.shudai.largescreen.service.OperatorAnalysisService;
import com.shudai.largescreen.util.DateQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class OperatorAnalysisServiceImpl implements OperatorAnalysisService {

    private  final OperatorAnalysisMapper operatorAnalysisMapper;

    @Override
    public String getAllDeal() {
        //获取三个月 第一天
        String mothStartLast = DateQuery.getMothStartLast2();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(mothStartLast, df);
        //查询三个月的 成交客户数量
        return operatorAnalysisMapper.findAllDeal3(parse);
    }

    @Override
    public List<IndustryShareVo> getIndustryShareMax(String province, String region) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)){
            province = "广东省";
            region = "深圳市";
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<IndustryShareVo> industryShareVos = new ArrayList<>();
        //当月
        String mothStart = DateQuery.getMothStart();
        String mothEnd = DateQuery.getMothEnd();
        LocalDateTime startMoth = LocalDateTime.parse(mothStart, df);
        LocalDateTime endMoth = LocalDateTime.parse(mothEnd, df);
        String mothNow = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])) + "月";
        IndustryShareVo industryShareVo = getIndustryShareVo(startMoth, endMoth, province, region, mothNow);
        industryShareVos.add(industryShareVo);

        //上月 第一天和最后一天
        String mothStartLast = DateQuery.getMothStartLast();
        String mothEndLast = DateQuery.getMothEndLast();
        LocalDateTime startMothLsat = LocalDateTime.parse(mothStartLast, df);
        LocalDateTime endMothLast = LocalDateTime.parse(mothEndLast, df);
        String mothLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-1) + "月";
        IndustryShareVo industryShareVoLast = getIndustryShareVo(startMothLsat, endMothLast, province, region, mothLast);
        industryShareVos.add(industryShareVoLast);

        //这上上月 第一天和最后一天
        String mothStartLastLast = DateQuery.getMothStartLast2();
        String mothEndLastLast = DateQuery.getMothEndLast2();
        LocalDateTime startMothLastLast = LocalDateTime.parse(mothStartLastLast, df);
        LocalDateTime endMothLastLast = LocalDateTime.parse(mothEndLastLast, df);
        String mothLastLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-2) + "月";
        IndustryShareVo industryShareVoLastLast = getIndustryShareVo(startMothLastLast, endMothLastLast, province, region, mothLastLast);
        industryShareVos.add(industryShareVoLastLast);

        return industryShareVos;
    }

    public IndustryShareVo getIndustryShareVo(LocalDateTime startMoth,LocalDateTime endMoth,String province,String  region,String moth){

        //总订单
        List<IndustryShareVo> allTotal = operatorAnalysisMapper.findAllDealNow(startMoth,endMoth,province,region);
        if (CollectionUtils.isNotEmpty(allTotal)){
            //最多
            String typeName = allTotal.get(0).getTypeName();
            Integer customerNumber = allTotal.get(0).getCustomerNumber();
            //总订单
            int sum = allTotal.stream().mapToInt(IndustryShareVo::getCustomerNumber).sum();
            IndustryShareVo industryShareVo = new IndustryShareVo();
            if (StringUtils.isNotEmpty(region)){
                industryShareVo.setArea(region);
            }else {
                industryShareVo.setArea(province);
            }

            industryShareVo.setCustomerNumber(customerNumber);
            industryShareVo.setDate(moth);
            industryShareVo.setTypeName(typeName);
            industryShareVo.setPercent(new BigDecimal(customerNumber).divide(new BigDecimal(sum),2, RoundingMode.HALF_UP));
            return industryShareVo;
        }else {
            IndustryShareVo industryShareVo = new IndustryShareVo();
            if (StringUtils.isNotEmpty(region)){
                industryShareVo.setArea(region);
            }else {
                industryShareVo.setArea(province);
            }
            industryShareVo.setCustomerNumber(0);
            industryShareVo.setDate(moth);
            industryShareVo.setTypeName("暂无");
            industryShareVo.setPercent(new BigDecimal(0));
            return industryShareVo;
        }

    }


    @Override
    public List<IndustryShareDetailVo> getIndustryShareDetail(String province, String region) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)){
            province = "广东省";
            region = "深圳市";
        }

        List<IndustryShareDetailVo> industryShareDetailVos = new ArrayList<>();
        //得到餐饮类别
        List<String> allTypeName = operatorAnalysisMapper.findAllTypeName();
        int id = 1;
        for (String type : allTypeName) {
            IndustryShareDetailVo industryShareDetailVo = getIndustryShareDetail(type,province,region);
            industryShareDetailVo.setId(id+"");
            industryShareDetailVos.add(industryShareDetailVo);
            id++;
        }

        return industryShareDetailVos;
    }

    public IndustryShareDetailVo getIndustryShareDetail(String type,String province, String region){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //当月
        String mothStart = DateQuery.getMothStart();
        String mothEnd = DateQuery.getMothEnd();
        LocalDateTime startMoth = LocalDateTime.parse(mothStart, df);
        LocalDateTime endMoth = LocalDateTime.parse(mothEnd, df);
        String mothNow = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])) + "月";

        Map<String, Object> rate = getRate(startMoth, endMoth, province, region, type);

        IndustryShareDetailVo industryShareDetailVo = new IndustryShareDetailVo();
        industryShareDetailVo.setCustomerMonth3(mothNow);

        industryShareDetailVo.setTradeCustomerNumber3((Integer) rate.get("typeNow"));
        industryShareDetailVo.setTradeCustomerPercent3(new BigDecimal(rate.get("rate")+""));

        //上月 第一天和最后一天
        String mothStartLast = DateQuery.getMothStartLast();
        String mothEndLast = DateQuery.getMothEndLast();
        LocalDateTime startMothLsat = LocalDateTime.parse(mothStartLast, df);
        LocalDateTime endMothLast = LocalDateTime.parse(mothEndLast, df);
        String mothLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-1) + "月";

        Map<String, Object> rate1 = getRate(startMothLsat, endMothLast, province, region, type);
        industryShareDetailVo.setCustomerMonth2(mothLast);
        industryShareDetailVo.setTradeCustomerNumber2((Integer) rate1.get("typeNow"));
        industryShareDetailVo.setTradeCustomerPercent2(new BigDecimal(rate1.get("rate")+""));


        //这上上月 第一天和最后一天
        String mothStartLastLast = DateQuery.getMothStartLast2();
        String mothEndLastLast = DateQuery.getMothEndLast2();
        LocalDateTime startMothLastLast = LocalDateTime.parse(mothStartLastLast, df);
        LocalDateTime endMothLastLast = LocalDateTime.parse(mothEndLastLast, df);
        String mothLastLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-2) + "月";

        Map<String, Object> rate2 = getRate(startMothLastLast, endMothLastLast, province, region, type);
        industryShareDetailVo.setCustomerMonth1(mothLastLast);
        industryShareDetailVo.setTradeCustomerNumber1((Integer) rate2.get("typeNow"));
        industryShareDetailVo.setTradeCustomerPercent1(new BigDecimal(rate2.get("rate")+""));
        industryShareDetailVo.setTypeAndMonth1(type+"/"+mothLastLast);
        //总
        int allMoth = (Integer)rate.get("typeAllNow")+(Integer)rate1.get("typeAllNow")+(Integer)rate2.get("typeAllNow");
        //分
        int typeMoth = (Integer)rate.get("typeNow")+(Integer)rate.get("typeNow")+(Integer)rate.get("typeNow");

        industryShareDetailVo.setTradeCustomerNumberTotal(typeMoth);
        if (allMoth != 0){
            industryShareDetailVo.setTradeCustomerPercentTotal(new BigDecimal(typeMoth).divide(new BigDecimal(allMoth),2, RoundingMode.HALF_UP));
        }else {
            industryShareDetailVo.setTradeCustomerPercentTotal(new BigDecimal(0));
        }

        industryShareDetailVo.setCustomerType(type);
        return industryShareDetailVo;
    }

    public  Map<String ,Object>  getRate(LocalDateTime startMoth,LocalDateTime endMoth,String province,String region,String type){
        //分
        Integer typeNow = operatorAnalysisMapper.findTypeNow(startMoth, endMoth, province, region, type);
        //总
        Integer typeAllNow = operatorAnalysisMapper.findTypeAllNow(startMoth, endMoth, province, region);
        if (typeAllNow != 0){
            BigDecimal rate = new BigDecimal(typeNow).divide(new BigDecimal(typeAllNow),2, RoundingMode.HALF_UP);
            Map<String ,Object> map = new HashMap<>();
            map.put("typeNow",typeNow);
            map.put("rate",rate);
            map.put("typeAllNow",typeAllNow);
            return map;
        }else {
            Map<String ,Object> map = new HashMap<>();
            map.put("typeNow",0);
            map.put("rate",new BigDecimal(0));
            map.put("typeAllNow",0);
            return map;
        }

    }

}
